"""
This script creates the HTML from the generated reST documents. This script MUST be ran after index.py.
"""

from {{ module.lower() }} import {{ module }}
from {{ module.lower() }}.shell import shell
import os, shutil

# Gets directories
html_dir = {{ module }}.get_html_directory()
src_dir = os.path.normpath(html_dir +'/source')

# Change to output dir. This is only required for remote output directories. However, it doesn't hurt the local.
cwd = os.getcwd()
os.chdir(html_dir)

# deletes all previous output.
# This seems to be required only for remote output directories.
for _dir in ['html', 'doctrees']:
	curr_dir = os.path.normpath(html_dir + '/build/' + _dir)
	if os.path.exists(curr_dir):
		print "Deleting "+ curr_dir
		try:
			shutil.rmtree(curr_dir)
		except:
			print "Warning!! "+ curr_dir + " could not be deleted!"
			# raise

#run sphinx html generation
shell("make html")

os.chdir(cwd)
os.chdir({{ module }}.get_project_root())

#this copies the makefile docs to the help folder for test results. This is done primary to give easy access to the documentation.
if os.path.exists(html_dir+'/build/html/help/makefile'):
	shutil.rmtree(html_dir+'/build/html/help/makefile')
	shutil.copytree(os.path.abspath({{ module }}.get_project_root()+'/doc/makefile_doc/build/html'), os.path.abspath(html_dir+'/build/html/help/makefile'))

print "Done."

